package org.jks.schedule;

import java.io.File;
import java.util.Date;
import java.util.ResourceBundle;

import javax.servlet.ServletContext;

import org.jks.constant.SysConfig;
import org.jks.util.DBUtil;
import org.jks.util.DateUtil;
import org.jks.util.FileUtil;
import org.springframework.web.context.ContextLoader;


public class BackupSchedule {

	public void execute(){
		ResourceBundle rb = ResourceBundle.getBundle("config");
		Date now = new Date();
		String curDate = DateUtil.toString(now, "yyyyMMdd");
		ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
		String basePath = servletContext.getRealPath("/") + File.separatorChar + rb.getString("backup.bakpath");
		String path = basePath + File.separator + curDate;
		FileUtil.mkdir(path);
		String bakDbPath = path + File.separator + "db";
		FileUtil.mkdir(bakDbPath);
		
		String binPath = rb.getString("backup.binpath");
		String username = rb.getString("jdbc.username");
		String password = rb.getString("jdbc.password");
		String dbname = rb.getString("backup.dbname");
		
		DBUtil.backup(binPath, username, password, dbname, bakDbPath);
		
		String bakFilePath = path + File.separator + "files";
		String source1 = servletContext.getRealPath("/") + File.separator + SysConfig.ATTACHEMENT;
		String source2 = servletContext.getRealPath("/") + File.separator + SysConfig.ASSESS;
		String target1 = bakFilePath + File.separator + SysConfig.ATTACHEMENT;
		String target2 = bakFilePath + File.separator + SysConfig.ASSESS;
		FileUtil.copy(source1, target1);
		FileUtil.copy(source2, target2);
	}
	
}
